require 'MVVM.Sources.View.Adaptor'

TextFieldAdaptor = class('TextFieldAdaptor', Adaptor)

function TextFieldAdaptor:ctor(gameObject, propertyName, binder, func,needRebuild)
    self.component = gameObject:GetComponent('Text')
    self.func = func
    self.needRebuild = needRebuild
    self:BindProperty(propertyName, binder)
end


function TextFieldAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if self.func == nil then
        self.component.text = tostring(newValue)
    else
        self.component.text = self.func(newValue)
    end
    if self.needRebuild then
        UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.component.transform)
        local parentTrans = self.component.transform.parent
        if utils.IsAvailableGameObject(parentTrans) then
            coroutine.start(function()
                coroutine.step()
                    UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(parentTrans)
            end)
        end

    end
end